import axios from 'axios'
import { handleConfigureAuth, handleNetworkError, handleNoResponseError, handlerRefreshToken } from './tools'

const axiosServer = axios.create({
	// axios中请求配置有baseURL选项，表示请求URL公共部分
	baseURL: import.meta.env.VITE_APP_BASE_API,
	timeout: 3000
})

//请求拦截器
axiosServer.interceptors.request.use(
	config => handleConfigureAuth(config), // 设置token到authorization头部
	error => Promise.reject(error)
)

//响应拦截器
axiosServer.interceptors.response.use(
	response => handlerRefreshToken(response),
	error => {
		const { response } = error
		if (response) {
			return handleNetworkError(response.status)
		} else {
			return handleNoResponseError(error)
		}
	}
)
export default axiosServer
